/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package me.aviator.service.impl;

import java.util.List;
import me.aviator.dao.CommonDao;
import me.aviator.entity.Ribbon;
import me.aviator.entity.myColor;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Administrator
 */
public class RibbonService {
    private final CommonDao commonDao;
    
    public RibbonService(Session session){
        commonDao=new CommonDao(session);
    }
    
    public List<Ribbon> listAll(){
        return commonDao.find(Ribbon.class);
    }
    
    public List<Ribbon> listByCatagory(String groupName){
        Criterion criterion=Restrictions.eq("catagory", groupName);
        return commonDao.find(Ribbon.class,criterion);
    }
    
    public List<Ribbon> ListByTone(String toneName){
        Criterion criterion=Restrictions.eq("tone", toneName);
        return commonDao.find(Ribbon.class,criterion);
    }
    
    public List<Ribbon> ListByNum(int num){
        Criterion criterion=Restrictions.eq("num", num);
        return commonDao.find(Ribbon.class,criterion);
    }
    
    public Ribbon findRibbonById(int id){
        Criterion criterion = Restrictions.eq("id", id);
		
        List<Ribbon> robbonlist = commonDao.find(Ribbon.class, criterion);
		
        if(robbonlist.size() == 0){
            return null;
        }
		
        return robbonlist.get(0);
    }
    
    public void uploadRibbon(String rName,String tone,String catagory,int num,List<myColor> colorStrap){
        Ribbon ribbon=new Ribbon();
        ribbon.setRname(rName);
        ribbon.setTone(tone);
        ribbon.setCatagory(catagory);
        ribbon.setNum(num);
        ribbon.setColorlist(colorStrap);
        commonDao.save(ribbon);
    }
    
    public void addRibbon(Ribbon ribbon){
        commonDao.save(ribbon);
    }
    
    public void removeRibbon(Ribbon ribbon){
        commonDao.delete(ribbon);
    }
    
    public Ribbon findRibbonByName(String rName){
        Criterion criterion = Restrictions.eq("Rname",rName);
		
	List<Ribbon> robbonlist = commonDao.find(Ribbon.class, criterion);
		
	if(robbonlist.size() == 0){
            return null;
	}
		
	return robbonlist.get(0);
    }
}
